﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Text;
using DBUtility;
using Model;
using MySql.Data.MySqlClient;

namespace DAL
{
    public class DUser
    {
        public bool CheckUser(string userName, string password)
        {

            string SQL = "SELECT UserName FROM tUser where UserName=@UserName and PassWord=@PassWord";
            try
            {
                MySqlParameter[] parameters = {
					new MySqlParameter("@UserName", MySqlDbType.VarChar,45),
					new MySqlParameter("@PassWord", MySqlDbType.VarChar,200)};
                parameters[0].Value = userName;
                parameters[1].Value = password;
                MySqlDataReader dataReader = DBUtility.DbHelperMySQL.ExecuteReader(SQL, parameters);
                if (dataReader == null) return false;
                bool result = dataReader.Read();
                dataReader.Close();
                return result;
            }
            catch (Exception ex)
            {
                Common.CommonRoutine.HandlingExceptions(ex);
                return false;
            }

        }

        public UserInfo CheckUserWithReturn(string userName, string password)
        {

            try
            {
                DataSet ds = GetList(" UserName='" + userName
                + "' and PassWord='" + password + "'");
                if (ds == null || ds.Tables.Count == 0 || ds.Tables[0].Rows.Count == 0)
                {
                    return null;
                }
                DataRow row = ds.Tables[0].Rows[0];
                UserInfo model = new UserInfo();
                if (row["UserName"] != null && row["UserName"].ToString() != "")
                {
                    model.UserName = row["UserName"].ToString();
                }
                if (row["PassWord"] != null && row["PassWord"].ToString() != "")
                {
                    model.Password = row["PassWord"].ToString();
                }
                if (row["Roleid"] != null && row["Roleid"].ToString() != "")
                {
                    model.Roleid = int.Parse(row["Roleid"].ToString());
                }
                if (row["workid"] != null && row["workid"].ToString() != "")
                {
                    model.workid = row["workid"].ToString();
                }
               
                if (row["RoleName"] != null && row["RoleName"].ToString() != "")
                {
                    model.RoleName = row["RoleName"].ToString();
                }
                if (row["Permission"] != null && row["Permission"].ToString() != "")
                {
                    model.Permission = row["Permission"].ToString();
                }
                return model;
            }
            catch (Exception ex)
            {
                return null;
            }
        }


        /// <summary>
        /// 得到最大ID
        /// </summary>
        public int GetMaxId()
        {
            return DbHelperMySQL.GetMaxID("id", "tuser");
        }

        /// <summary>
        /// 是否存在该记录
        /// </summary>
        public bool Exists(string userName)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) from tuser");
            strSql.Append(" where userName=@userName");
            MySqlParameter[] parameters = {
					new MySqlParameter("@userName", MySqlDbType.String)
};
            parameters[0].Value = userName;

            return DbHelperMySQL.Exists(strSql.ToString(), parameters);
        }

        public bool ChangePassWordByUserName(string userName, string newPwd)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update tuser set ");
            strSql.Append("Password=@Password");
            strSql.Append(" where UserName=@UserName");
            MySqlParameter[] parameters = {
					new MySqlParameter("@PassWord", MySqlDbType.VarChar,200),
					new MySqlParameter("@UserName", MySqlDbType.VarChar,45)};
            parameters[0].Value = newPwd;
            parameters[1].Value = userName;
            int rows = DbHelperMySQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }

        }

        /// <summary>
        /// 增加一条数据
        /// </summary>
        public bool Add(Model.UserInfo model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("insert into tuser(");
            strSql.Append("UserName,Password,Roleid,workid,Remark)");
            strSql.Append(" values (");
            strSql.Append("@UserName,@Password,@Roleid,@workid,@Remark)");
            MySqlParameter[] parameters = {
					new MySqlParameter("@UserName", MySqlDbType.VarChar,45),
					new MySqlParameter("@Password", MySqlDbType.VarChar,200),
					new MySqlParameter("@Roleid", MySqlDbType.Int32,10),
					new MySqlParameter("@workid", MySqlDbType.VarChar,45),
					new MySqlParameter("@Remark", MySqlDbType.Text)};
            parameters[0].Value = model.UserName;
            parameters[1].Value = model.Password;
            parameters[2].Value = model.Roleid;
            parameters[3].Value = model.workid;
            parameters[4].Value = model.Remark;

            int rows = DbHelperMySQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        /// <summary>
        /// 更新一条数据
        /// </summary>
        public bool Update(Model.UserInfo model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update tuser set ");
            strSql.Append("UserName=@UserName,");
            strSql.Append("Password=@Password,");
            strSql.Append("Roleid=@Roleid,");
            strSql.Append("workid=@workid,");
            strSql.Append("Remark=@Remark");
            strSql.Append(" where id=@id");
            MySqlParameter[] parameters = {
					new MySqlParameter("@UserName", MySqlDbType.VarChar,45),
					new MySqlParameter("@Password", MySqlDbType.VarChar,200),
					new MySqlParameter("@Roleid", MySqlDbType.Int32,10),
					new MySqlParameter("@workid", MySqlDbType.VarChar,45),
					new MySqlParameter("@Remark", MySqlDbType.Text),
					new MySqlParameter("@id", MySqlDbType.Int32,10)};
            parameters[0].Value = model.UserName;
            parameters[1].Value = model.Password;
            parameters[2].Value = model.Roleid;
            parameters[3].Value = model.workid;
            parameters[4].Value = model.Remark;
            parameters[5].Value = model.id;

            int rows = DbHelperMySQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// 删除一条数据
        /// </summary>
        public bool Delete(int id)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from tuser ");
            strSql.Append(" where id=@id");
            MySqlParameter[] parameters = {
					new MySqlParameter("@id", MySqlDbType.Int32)
};
            parameters[0].Value = id;

            int rows = DbHelperMySQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        /// <summary>
        /// 批量删除数据
        /// </summary>
        public bool DeleteList(string idlist)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from tuser ");
            strSql.Append(" where id in (" + idlist + ")  ");
            int rows = DbHelperMySQL.ExecuteSql(strSql.ToString());
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }


        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public Model.UserInfo GetModel(int id)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("select id,UserName,Password,Roleid,workid,Remark from tuser ");
            strSql.Append(" where id=@id");
            MySqlParameter[] parameters = {
					new MySqlParameter("@id", MySqlDbType.Int32)};
            parameters[0].Value = id;

            Model.UserInfo model = new Model.UserInfo();
            DataSet ds = DbHelperMySQL.Query(strSql.ToString(), parameters);
            if (ds.Tables[0].Rows.Count > 0)
            {
                if (ds.Tables[0].Rows[0]["id"] != null && ds.Tables[0].Rows[0]["id"].ToString() != "")
                {
                    model.id = int.Parse(ds.Tables[0].Rows[0]["id"].ToString());
                }
                if (ds.Tables[0].Rows[0]["UserName"] != null && ds.Tables[0].Rows[0]["UserName"].ToString() != "")
                {
                    model.UserName = ds.Tables[0].Rows[0]["UserName"].ToString();
                }
                if (ds.Tables[0].Rows[0]["Password"] != null && ds.Tables[0].Rows[0]["Password"].ToString() != "")
                {
                    model.Password = ds.Tables[0].Rows[0]["Password"].ToString();
                }
                if (ds.Tables[0].Rows[0]["Roleid"] != null && ds.Tables[0].Rows[0]["Roleid"].ToString() != "")
                {
                    model.Roleid = int.Parse(ds.Tables[0].Rows[0]["Roleid"].ToString());
                }
                if (ds.Tables[0].Rows[0]["workid"] != null && ds.Tables[0].Rows[0]["workid"].ToString() != "")
                {
                    model.workid = ds.Tables[0].Rows[0]["workid"].ToString();
                }
                if (ds.Tables[0].Rows[0]["Remark"] != null && ds.Tables[0].Rows[0]["Remark"].ToString() != "")
                {
                    model.Remark = ds.Tables[0].Rows[0]["Remark"].ToString();
                }
                return model;
            }
            else
            {
                return null;
            }
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetList(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select tuser.id,tuser.UserName,tuser.Password,tuser.Roleid,tuser.workid,tuser.Remark,trole.RoleName,trole.Permission ");
            strSql.Append(" FROM tuser left join trole on tuser.Roleid = trole.id;");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            return DbHelperMySQL.Query(strSql.ToString());
        }

        /// <summary>
        /// 获取记录总数
        /// </summary>
        public int GetRecordCount(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) FROM tuser ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            object obj = DbHelperSQL.GetSingle(strSql.ToString());
            if (obj == null)
            {
                return 0;
            }
            else
            {
                return Convert.ToInt32(obj);
            }
        }

    }
}
